package com.sofeteem.ikun.view;

import com.sofeteem.ikun.model.Bullet;
import com.sofeteem.ikun.model.Enemy;
import com.sofeteem.ikun.model.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class GamePanel extends JPanel implements ActionListener {

    private Timer timer;
    private Player player;
    private List<Enemy> enemies;
    private List<Bullet> bullets;
    private Random random;
    private boolean isGameOver;
    private int score;

    public GamePanel() {
        initGame();
    }

    private void initGame() {
        setPreferredSize(new Dimension(800, 600));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(new TAdapter());

        player = new Player();
        enemies = new ArrayList<>();
        bullets = new ArrayList<>();
        random = new Random();
        isGameOver = false;
        score = 0;

        timer = new Timer(10, this);
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (!isGameOver) {
            drawPlayer(g);
            drawEnemies(g);
            drawBullets(g);
            drawUI(g);
        } else {
            drawGameOver(g);
        }
    }

    private void drawPlayer(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillPolygon(player.getTriangle());
    }

    private void drawEnemies(Graphics g) {
        for (Enemy enemy : enemies) {
            g.setColor(Color.RED);
            g.fillRect(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
        }
    }

    private void drawBullets(Graphics g) {
        for (Bullet bullet : bullets) {
            g.setColor(Color.YELLOW);
            g.fillOval(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
        }
    }

    private void drawUI(Graphics g) {
        g.setColor(Color.WHITE);
        g.drawString("Score: " + score, 10, 20);
        g.drawString("Health: " + player.getHealth(), 10, 40);
        g.drawString("Energy: " + player.getEnergy(), 10, 60);
    }

    private void drawGameOver(Graphics g) {
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 40));
        g.drawString("Game Over", 300, 300);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!isGameOver) {
            movePlayer();
            moveEnemies();
            moveBullets();
            checkCollisions();
            spawnEnemies();
            removeOffScreenBullets();
            removeOffScreenEnemies();
        }
        repaint();
    }

    private void movePlayer() {
        player.move();
    }

    private void moveEnemies() {
        for (Enemy enemy : enemies) {
            enemy.move();
        }
    }

    private void moveBullets() {
        for (Bullet bullet : bullets) {
            bullet.move();
        }
    }

    private void checkCollisions() {
        Iterator<Bullet> bulletIterator = bullets.iterator();
        while (bulletIterator.hasNext()) {
            Bullet bullet = bulletIterator.next();
            Iterator<Enemy> enemyIterator = enemies.iterator();
            while (enemyIterator.hasNext()) {
                Enemy enemy = enemyIterator.next();
                if (bullet.getBounds().intersects(enemy.getBounds())) {
                    enemy.takeDamage(bullet.getDamage());
                    bulletIterator.remove();
                    if (enemy.getHealth() <= 0) {
                        enemyIterator.remove();
                        score += 100;
                        player.addEnergy(20);
                    }
                    break;
                }
            }
        }

        for (Enemy enemy : enemies) {
            if (player.getBounds().intersects(enemy.getBounds())) {
                player.takeDamage(10);
                if (player.getHealth() <= 0) {
                    isGameOver = true;
                    timer.stop(); // Stop the game timer
                }
            }
        }
    }

    private void spawnEnemies() {
        if (random.nextInt(100) < 3) { // 3% chance to spawn an enemy
            enemies.add(new Enemy(random.nextInt(getWidth()), -50));
        }
    }

    private void removeOffScreenBullets() {
        Iterator<Bullet> iterator = bullets.iterator();
        while (iterator.hasNext()) {
            Bullet bullet = iterator.next();
            if (bullet.getY() < 0) {
                iterator.remove();
            }
        }
    }

    private void removeOffScreenEnemies() {
        Iterator<Enemy> iterator = enemies.iterator();
        while (iterator.hasNext()) {
            Enemy enemy = iterator.next();
            if (enemy.getY() > getHeight()) {
                iterator.remove();
            }
        }
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
            int key = e.getKeyCode();

            if (key == KeyEvent.VK_SPACE) {
                bullets.add(new Bullet(player.getX() + player.getWidth() / 2, player.getY()));
            }

            if (key == KeyEvent.VK_R && player.getEnergy() == 100) {
                player.useSkill();
                for (int i = 0; i < getWidth(); i += 20) {
                    bullets.add(new Bullet(i, getHeight()));
                }
                player.setEnergy(0);
            }
        }
    }
}
